#include <math.h>

#include "Counter.h"
#include "SimpleText.h"
#include "GameObject.h"
#include "Helpers.hpp"

using namespace game_engine;
using namespace game_engine::library;
using namespace game_engine::components;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

Counter::Counter(void)
    : Behaviour(), mode(CHRONOMETER), value(0.0f), state(NULL)
{
    type = "Counter";
}

Counter::Counter(GameObject *obj)
    : Behaviour(obj), mode(CHRONOMETER), value(0.0f), state(NULL)
{
    type = "Counter";
}

Counter::~Counter(void)
{
    delete state;
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void Counter::initialize(void) throw(...)
{
    // Create states for this behaviour
    state = new MainCounterState(this);
    setCurrentState(state);

    // Base class initialization
    Behaviour::initialize();
}

///////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////

const TimerMode& Counter::getMode(void) const
{
	return mode;
}

const FLOAT32 Counter::getValue(void) const
{
    return value;
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void Counter::setMode(const TimerMode &m)
{
	mode = m;
}

void Counter::setValue(const FLOAT32 val)
{
    value = val;

    // Fire signal
    signalValueUpdated.Fire(value);

    // Update the SimpleText component
    SimpleText *txt = dynamic_cast<SimpleText*> (object->getComponent("SimpleText"));
    txt->setText(to_string<INT32>(floor(value), right));
}
